package com.sunnada.edu.system.quartz.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.quartz.SchedulerException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.common.LogInfo;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.filter.XssHttpServletRequestWrapper;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.quartz.bean.Quartz;
import com.sunnada.edu.system.quartz.service.QuartzService;


/**
 * explain 
 * @author chenbin7270 2014-10-22
 * version 1.0
 */
@Controller
@RequestMapping("/quartzController")
public class QuartzController extends BasicController{
	@Resource
	public QuartzService quartzService;

	@RequestMapping("/quartz")
	public String quartz(HttpServletRequest request) throws IOException {
		String curr = request.getParameter("curr");
		request.setAttribute("curr", curr);
		// 当前路径
		String moduleId = request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		return "system/quartz/quartz";
	}	
	
	@RequestMapping("/quartzSearch")
	public void quartzSearch(HttpServletResponse response,HttpServletRequest request) throws IOException{
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Quartz.class, params);
		Page page = new Page();
		page.setRowCount(quartzService.findQuartzCot(params));
		page.setPageMap(request, page, params);
		List quartz = quartzService.findQuartzs(params);
		JSONObject map = new JSONObject();
		map.put("list", quartz);
	    map.put("params", params);
		printInfo(response,map);
	}
	
	@RequestMapping("/quartzSave")
	public void quartzSave(HttpServletResponse response,HttpServletRequest request) throws SchedulerException, ClassNotFoundException {
		HashMap<String, String> params = new HashMap<String, String>();
		HttpServletRequest re=XssHttpServletRequestWrapper.getOrgRequest(request);
		setReqParams(re, Quartz.class, params);
		String cron =params.get("cron");
		String result="";
		if(org.quartz.CronExpression.isValidExpression(cron)){
		    result= quartzService.quartzUpdate(params);
	        quartzService.deleteJob(params);
	        quartzService.rescheduleJob(params);
	        //LogInfo.getInstance().info(request,QuartzController.class,"[定时器管理][定时器管理修改]["+params.get("recId")+"]"); 
		}else{
		    result= "{\"success\":\"" + false + "\",\"msg\":\"" + "表达式不符合格式要求！" + "\"}";
		}
		printInfo(response,result);
	}

	@RequestMapping("/quartzUpdate")
	public String quartzeUpdate(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Quartz.class, params);
		List quartz = quartzService.getQuartz(params);
		request.setAttribute("quartz", quartz);
		//LogInfo.getInstance().info(request,QuartzController.class,"[定时器管理][显示修改信息]["+params.get("recId")+"]");
		return "system/quartz/quartz_update";
	}
	
	
	
    /**
     * explain 暂停定时任务
     * @throws IOException 
     * @throws SchedulerException 
     * @throws ClassNotFoundException 
     */
    @RequestMapping("/pauseJob")
    public void pauseJob(HttpServletRequest request,
            HttpServletResponse response) throws  SchedulerException, ClassNotFoundException {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("recId", request.getParameter("recId"));
        params.put("jobId", request.getParameter("recId"));
        params.put("jobStatus", request.getParameter("jobStatus"));
        msg =quartzService.pauseJob(params);
        quartzService.deleteJob(params);
        if("success".equals(msg)){
            msg="暂停成功";
        }else{
            msg="暂停失败";
        }
       // LogInfo.getInstance().info(request, Quartz.class,"[定时任务管理][暂停定时任务]" + "[任务编号：" + params.get("recId") + "]");      
        printInfo(response, msg); 
    }
    
    /**
     * explain 恢复定时任务
     * @throws IOException 
     * @throws SchedulerException 
     * @throws ClassNotFoundException 
     */
    @RequestMapping("/resumeJob")
    public void resumeJob(HttpServletRequest request,
            HttpServletResponse response) throws  SchedulerException, ClassNotFoundException {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("recId", request.getParameter("recId"));
        params.put("jobId", request.getParameter("recId"));
        params.put("jobStatus", request.getParameter("jobStatus"));
        msg =quartzService.resumeJob(params);
        quartzService.deleteJob(params);
        quartzService.rescheduleJob(params);
        if("success".equals(msg)){
            msg="恢复成功";
        }else{
            msg="恢复失败";
        }
       // LogInfo.getInstance().info(request, Quartz.class,"[定时任务管理][恢复定时任务]" + "[任务编号：" + params.get("recId") + "]");      
        printInfo(response, msg); 
    }
    
    /**
     * explain 删除定时任务
     * @throws IOException 
     * @throws SchedulerException 
     */
    @RequestMapping("/deleteJob")
    public void deleteJob(HttpServletRequest request,
            HttpServletResponse response) throws  SchedulerException {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("recId", request.getParameter("recId"));
        try {
            msg =quartzService.deleteJob(params);
        } finally{
            if("success".equals(msg)){
                msg="删除成功";
            }else{
                msg="删除失败";
            }
          //  LogInfo.getInstance().info(request, Quartz.class,"[定时任务管理][删除定时任务]" + "[任务编号：" + params.get("recId") + "]");      
            printInfo(response, msg); 
        }
    }
    
    
    /**
     * explain 重新执行定时任务
     * @throws IOException 
     * @throws SchedulerException 
     * @throws ClassNotFoundException 
     */
    @RequestMapping("/rescheduleJob")
    public void rescheduleJob(HttpServletRequest request,
            HttpServletResponse response) throws  SchedulerException, ClassNotFoundException {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("recId", request.getParameter("recId"));
        try {
            msg =quartzService.rescheduleJob(params);
        } finally {
            if("success".equals(msg)){
                msg="重新执行成功";
            }else{
                msg="重新执行失败";
            }
           // LogInfo.getInstance().info(request, Quartz.class,"[定时任务管理][重新执行定时任务]" + "[任务编号：" + params.get("recId") + "]");      
            printInfo(response, msg); 
        }
    }
    
    
    /**
     * explain 立即执行定时任务
     * @throws IOException 
     * @throws SchedulerException 
     */
    @RequestMapping("/triggerJob")
    public void triggerJob(HttpServletRequest request,
            HttpServletResponse response) throws  SchedulerException {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("recId", request.getParameter("recId"));
        try {
            msg =quartzService.triggerJob(params);
        } finally {
            if("success".equals(msg)){
                msg="立即执行成功";
            }else{
                msg="立即执行失败,请查看任务状态！";
            }
           // LogInfo.getInstance().info(request, Quartz.class,"[定时任务管理][立即执行一次定时任务]" + "[任务编号：" + params.get("recId") + "]");      
            printInfo(response, msg); 
        }
    }

 
	
}
